package com.yuelan.apartment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuelan.apartment.common.Constant;
import com.yuelan.apartment.domain.ApaApartment;
import com.yuelan.apartment.mapper.ApaApartmentMapper;
import com.yuelan.apartment.mapper.ApaRoomMapper;
import com.yuelan.apartment.service.ApaApartmentService;
import com.yuelan.common.exception.ServiceException;
import com.yuelan.common.utils.DateUtils;
import com.yuelan.common.utils.SecurityUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @Author: ZhaoYi
 * @date: 24-5-18 11:05
 */
@Service
public class ApaApartmentServiceImpl implements ApaApartmentService {

    @Resource
    private ApaApartmentMapper apaApartmentMapper;
    @Resource
    private ApaRoomMapper apaRoomMapper;


    @Override
    public int add(ApaApartment apaApartment){
        String createBy = SecurityUtils.getUsername();
        LambdaQueryWrapper<ApaApartment> apartmentWrapper = new LambdaQueryWrapper<>();
        apartmentWrapper.eq(ApaApartment::getCreateBy, createBy)
                .eq(ApaApartment::getApartmentName, apaApartment.getApartmentName());
        ApaApartment existApaApartment = apaApartmentMapper.selectOne(apartmentWrapper);
        if (existApaApartment != null) {
            throw new ServiceException("该房源昵称已存在");
        }
        apaApartment.setCreateBy(createBy);
        apaApartment.setUpdateBy(createBy);
        apaApartment.setCreateTime(DateUtils.getNowDate());
        apaApartment.setUpdateTime(DateUtils.getNowDate());
        return apaApartmentMapper.insert(apaApartment);
    }


    @Override
    @Transactional
    public int delete(Long id) {
        if (id == null) {
            throw new ServiceException(Constant.SERVICE_ERROR);
        }
        LambdaUpdateWrapper<ApaApartment> apartmentUpdateWrapper = new LambdaUpdateWrapper<>();
        apartmentUpdateWrapper.eq(ApaApartment::getId, id)
                .set(ApaApartment::getDeleted, 1);
        return apaApartmentMapper.update(apartmentUpdateWrapper);
    }


    @Override
    @Transactional
    public int deleteIds(Long[] ids) {
        if (ids == null) {
            throw new ServiceException(Constant.SERVICE_ERROR);
        }
        return apaApartmentMapper.updateBatchDeleted(ids);
    }


    @Override
    public int update(ApaApartment apaApartment) {
        if (apaApartment == null) {
            throw new ServiceException("from cannot null");
        }
        LambdaUpdateWrapper<ApaApartment> apaApartmentUpdateWrapper = new LambdaUpdateWrapper<>();
        apaApartmentUpdateWrapper.setEntity(apaApartment);
        return apaApartmentMapper.update(apaApartmentUpdateWrapper);
    }


    @Override
    public ApaApartment load(Long id) {
        if (id == null) {
            throw new ServiceException(Constant.SERVICE_ERROR);
        }
        LambdaQueryWrapper<ApaApartment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApaApartment::getId, id);
        return apaApartmentMapper.selectOne(queryWrapper);
    }


    @Override
    public List<ApaApartment> pageList(ApaApartment apaApartment) {
        LambdaQueryWrapper<ApaApartment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApaApartment::getCreateBy, SecurityUtils.getUsername());
        // 按房源昵称查询
        queryWrapper.like(apaApartment.getApartmentName() != null,
                ApaApartment::getApartmentName, apaApartment.getApartmentName());
        // 按房源地址查询
        queryWrapper.like(apaApartment.getAddress() != null,
                ApaApartment::getAddress, apaApartment.getAddress());
        // 按房源房产证编号查询
        queryWrapper.eq(apaApartment.getApartmentNum() != null,
                ApaApartment::getApartmentNum, apaApartment.getApartmentNum());
        return apaApartmentMapper.selectList(queryWrapper);
    }


    @Override
    public List<ApaApartment> allHouse() {
        LambdaQueryWrapper<ApaApartment> wrapperHouse = new LambdaQueryWrapper<ApaApartment>()
                .eq(ApaApartment::getCreateBy , SecurityUtils.getUsername());
        return apaApartmentMapper.selectList(wrapperHouse);
    }

}
